package com.todoapp.page;


import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import todo.core.domain.Todo;

import com.todoapp.Boolean2YesNoConverter;
import com.todoapp.CustomLabel;
import com.todoapp.RowAlternatorModel;


public class HomePage extends Template {


	private Todo selected = null;
	
	/**
	 * Define the ActionPanel (select, delete, edit)
	 *
	 */
	public class ActionPanel extends Panel
	{
		/**
		 * @param id
		 *            component id
		 * @param model
		 *            model for contact
		 */
		public ActionPanel(String id, IModel<Todo> model)
		{
			super(id, model);
			add(new Link("select")
			{
				@Override
				public void onClick()
				{
					selected = (Todo)getParent().getDefaultModelObject();
				}
			});
			add(new Link("delete")
			{
				@Override
				public void onClick()
				{
					Todo detachedPersistentInstance = (Todo)getParent().getDefaultModelObject();
					getTodoService().delete(detachedPersistentInstance);
					// force "new", so that the model is updated.
					// the alternative is to use a "refreshing view", but this is just a demo!
					setResponsePage(new HomePage()); 
				}
			});
			add(new Link("edit")
			{
				@Override
				public void onClick()
				{
					selected = (Todo)getParent().getDefaultModelObject();
					setResponsePage(new EditTodo(selected));
				}
			});
		}
	}
	
    public HomePage() {

        // Add the simplest type of label
        add(new Label("message",  new PropertyModel<String>(this, "message")));

        List<Todo> todoList = getTodoService().findAll();
        RepeatingView todosView = new RepeatingView("todos");
        
        add(todosView);
        
        RowAlternatorModel rowAlternator = new RowAlternatorModel();
        
        for (Todo todo : todoList) {
        	CompoundPropertyModel<Todo> propertyModel = new CompoundPropertyModel<Todo>(todo);
        	WebMarkupContainer item = new WebMarkupContainer(todosView.newChildId(), propertyModel);
        	todosView.add(item);
        	
       		item.add(new ActionPanel("actions", propertyModel));
       		
       		// here the title is matched to the CompoundPropertyModel
    		item.add(new Label("title"));
    		
    		// example of how the value from the model can be overridden.
    		//item.add( new Label("complete", todo.getComplete()?"Yes":"No"));
    		item.add( new CustomLabel("complete", new Model(todo.getComplete()), new Boolean2YesNoConverter()) );
    		
    		// alternating row color
			item.add( new AttributeModifier("class", true, rowAlternator) );
        }
        
        add(new Link("newTodoLink"){
        	public void onClick() {
        		setResponsePage(new EditTodo());
        	}
        });
    }
    /**
	 * Constructor that is invoked when page is invoked without a session.
	 * 
	 * @param parameters
	 *            Page parameters
	 */
    public HomePage(final PageParameters parameters) {
    		this();
    }
    
    @Override
    public String getPageTitle() {
    	// return this page's title
    	return "Home Page";
    }
	public Todo getSelected() {
		return selected;
	}
	public void setSelected(Todo selected) {
		this.selected = selected;
	}
	public String getMessage() {
		if (selected == null) {
			return "Nothing selected";
		} else {
			return getSelected().toString();
		}
	}
}
